/**
 * 小于10的数，十位自动填0
 * @param param 数字
 * @ return param
 */
function withData(param) {
    return param < 10 ? '0' + param : '' + param;
}
/** 
  * @param start 开始下标 end 结束下标
  * @return array 
  */
function getLoopArray(start, end) {
    var start = start || 0;
    var end = end || 1;
    var array = [];
    for (var i = start; i <= end; i++) {
        array.push(withData(i));
    }
    return array;
}
/** 
  * 获得当前月的天数
  * @param year 年 month 月
  * @return array 当前月的天数 ,数组类型
  */
function getMonthDay(year, month) {
    var flag = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0), array = null;

    switch (month) {
        case '01':
        case '03':
        case '05':
        case '07':
        case '08':
        case '10':
        case '12':
            array = getLoopArray(1, 31)
            break;
        case '04':
        case '06':
        case '09':
        case '11':
            array = getLoopArray(1, 30)
            break;
        case '02':
            array = flag ? getLoopArray(1, 29) : getLoopArray(1, 28)
            break;
        default:
            array = '月份格式不正确，请重新输入！'
    }
    return array;
}
/** 
  * 默认获取当前时间
  * @return array 当前的年月日 时分秒
  */
function getNewDateArry() {  
    var newDate = new Date();
    var year = withData(newDate.getFullYear()),
        mont = withData(newDate.getMonth() + 1),
        date = withData(newDate.getDate()),
        hour = withData(newDate.getHours()),
        minu = withData(newDate.getMinutes()),
        seco = withData(newDate.getSeconds());

    return [year, mont, date, hour, minu, seco];
}
/** 
  * @param startYear 最早年份 endYear 最晚年份 date (date要求输入格式 '1999-10-11 21:59:32')
  * @return object  dateTimeArray年月日时分秒的二维数组
  */
function dateTimePicker(startYear, endYear, date) {
    var dateTime = [], dateTimeArray = [[], [], [], [], [], []];
    var start = startYear || 1978;
    var end = endYear || 2100;
    var defaultDate = date ? [...date.split(' ')[0].split('-'), ...date.split(' ')[1].split(':')] : getNewDateArry();

    dateTimeArray[0] = getLoopArray(start, end);
    dateTimeArray[1] = getLoopArray(1, 12);
    dateTimeArray[2] = getMonthDay(defaultDate[0], defaultDate[1]);
    dateTimeArray[3] = getLoopArray(0, 23);
    dateTimeArray[4] = getLoopArray(0, 59);
    dateTimeArray[5] = getLoopArray(0, 59);

    dateTimeArray.forEach((current, index) => {
        dateTime.push(current.indexOf(defaultDate[index]));
    });
    return {
        dateTimeArray: dateTimeArray,
        dateTime: dateTime
    }
}
module.exports = {
    dateTimePicker: dateTimePicker,
    getMonthDay: getMonthDay
}